package com.beiding.codecreater.render.java.utils;

import java.util.HashSet;
import java.util.Set;

public class NameUtils {


    private static Set<Character> classNameAllowChars = new HashSet<>();

    private static Set<Character> classNameFirstNotAllowChars = new HashSet<>();

    private static Set<Character> packageNameAllowChars = new HashSet<>();

    private static Set<Character> packageNameFirstNotAllowChars = new HashSet<>();

    static {
        for (char i = 'a'; i <= 'z'; i++) {
            classNameAllowChars.add(i);
        }
        for (char i = 'A'; i <= 'Z'; i++) {
            classNameAllowChars.add(i);
        }

        for (char i = '0'; i <= '9'; i++) {
            classNameAllowChars.add(i);
            classNameFirstNotAllowChars.add(i);
        }

        classNameAllowChars.add('_');

        packageNameAllowChars.addAll(classNameAllowChars);
        packageNameAllowChars.add('.');
        packageNameFirstNotAllowChars.addAll(classNameFirstNotAllowChars);
        packageNameFirstNotAllowChars.add('.');

    }

    //检查包名是否有效
    public static void checkCommonName(String name) {

        if (name == null || name.length() == 0) {
            throw new RuntimeException("包名不能为空");
        }

        char[] chars = name.toCharArray();

        if (classNameFirstNotAllowChars.contains(chars[0])) {
            throw new RuntimeException("无效包名:" + name);
        }

        for (char c : chars) {
            if (!classNameAllowChars.contains(c)) {
                throw new RuntimeException("无效包名:" + name);
            }
        }
    }

    public static void checkPackageName(String name) {

        if (name == null || name.length() == 0) {
            throw new RuntimeException("包名不能为空");
        }

        char[] chars = name.toCharArray();

        if (packageNameFirstNotAllowChars.contains(chars[0])) {
            throw new RuntimeException("无效包名:" + name);
        }

        for (char c : chars) {
            if (!packageNameAllowChars.contains(c)) {
                throw new RuntimeException("无效包名:" + name);
            }
        }
    }
}
